<template>
  <div class="container">
    <div class="loginLogo">
      <el-image class="WanganLogo" :src="require('@/assets/img/wangan-logo.png')" fit="contain"></el-image>
      <span class="WanganTitle">{{$t('工业防火墙')}}</span>
    </div>
    <el-image class="WanganBackground" :src="require('@/assets/img/anhengBackground.jpg')" fit="fill"></el-image>
    <div class="textLogin">
      <div class="WanganText" style="text-align: center;">
        <div style="margin:0 20px 20px;">{{$t('工业品质、安全可靠')}}</div>
        <div style="margin:0 20px;">{{$t('高效·细粒·精准控制')}}</div>
      </div>
    </div>
    <div class="loginFormWangan">
      <h2>{{$t('用户登录')}}</h2>
      <el-form label-width="" :model="ruleForm" ref="ruleForm" :rules="loginRules" class="loginFormItem">
        <el-form-item label="" prop="username">
          <el-input v-emoji v-model="ruleForm.username" size="medium" prefix-icon="el-icon-user" :placeholder="$t('用户名')"></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input v-emoji type="password" size="medium" :placeholder="$t('密码')" prefix-icon="el-icon-lock" v-model="ruleForm.password" @keyup.enter.native="submitForm(ruleForm)"></el-input>
        </el-form-item>
        <el-button type="primary" class="loginWanganButton" :loading="loading" @click.native.prevent="submitForm(ruleForm)">{{$t('登录')}}</el-button>
        <div style="text-align: right;margin-top: 5px;">
          <el-link :underline="false" style="font-size: 12px;" @click="userAuthLoginClick">{{$t('用户认证')}}<i class="el-icon-arrow-right el-icon--right"></i> </el-link>
        </div>
      </el-form>
    </div>
    <div class="textCompany">
    </div>
  </div>
</template>

<script>
import { setRoleList } from '@/utils/auth'
import { getMenu } from '@/api/menu'
export default {
  name: 'LoginWangan',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入用户名')))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入密码')))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      loading: false,
      loginRules: {
        username: [{ required: false, trigger: 'blur', validator: validateUsername }],
        password: [{ required: false, trigger: 'blur', validator: validatePassword }]
      }
    }
  },
  methods: {
    submitForm (ruleForm) {
      let _that = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', ruleForm).then(res => {
            if (res.status === 0) {
              if (res.info[0].ukey !== 0) {
                this.OnLoadRead().then(r => {
                  setTimeout(function () {
                    _that.Arm_Enum_Read([res.info[0], _that.goMenu, _that.colseLoading])
                  }, 100)
                })
              } else {
                this.$store.dispatch('user/saveInfo', res.info[0]).then(() => {
                  getMenu({ roleID: res.info[0].roleID }).then((response) => {
                    if (response.status === 0) {
                      setRoleList(response.url_list)
                      this.$nextTick(() => {
                        this.loading = false
                        if (res.info[0].password_update_time === 0) {
                          this.$router.push('/changePassword')
                        } else {
                          this.$router.push('/')
                        }
                      })
                    }
                  }).catch(e => {
                    this.loading = false
                  })
                })
              }
              // this.$router.push('/network/physical/interface')
            } else {
              this.loading = false
            }
          }).catch(e => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
        }
      })
    },
    goMenu (info, status) {
      if (status !== 0) {
        this.loading = false
        this.$store.dispatch('user/logout')
        this.$messages.error(this.$t('UkeyAgain'))
      } else {
        this.$store.dispatch('user/saveInfo', info).then(() => {
          getMenu({ role: info.role }).then((response) => {
            if (response.status === 0) {
              setRoleList(response.url_list)
              this.$nextTick(() => {
                this.loading = false
                if (info.password_update_time === 0) {
                  this.$router.push('/changePassword')
                } else {
                  this.$router.push('/')
                }
              })
            }
          }).catch(e => {
            this.loading = false
          })
        })
      }
    },
    colseLoading () {
      this.loading = false
    },
    userAuthLoginClick () {
      const hrefUrl = window.location.href.match(/(http:\/\/|https:\/\/)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/)
      const url = hrefUrl[0] + ':8183/#/authLogin'
      window.open(url)
    }
  }
}
</script>

<style scoped>
.container {
  /* width: 40%; */
  position: relative;
  width: 100%;
  height: 100vh;
  background-size: cover;
  /* padding: 1px; */
  box-sizing: border-box;
  z-index: 1;
  /* overflow: hidden; */
  /* background-color: rgb(255, 255, 255, 0.5); */
}
.WanganBackground {
  position: absolute;
  filter: blur(3px);
  z-index: 1;
  width: 100%;
  height: 100vh;
}

.loginFormWangan {
  position: absolute;
  padding-top: 20px;
  left: calc(50vw - 350px);
  top: calc(50vh - 120px - (5vh + 2vw));
  width: 400px;
  height: 330px;
  text-align: center;
  z-index: 1001;
  background-color: #f7f7f6;
  border-radius: 3px;
  box-shadow: 0 0 10px 3px rgb(39 37 37 / 45%);
  animation: AllInRight;
  animation-duration: 0.6s;
}

.loginFormItem {
  margin: 30px auto 10px;
  width: 280px;
}
.loginLogo {
  position: absolute;
  z-index: 1001;
  top: calc(50vh - 260px - (1vh + 2vw));
  width: 100%;
  font-size: 0;
  text-align: center;
  color: #ffffff;
}
.WanganLogo {
  /* height: calc(5vh + 2vw); */
  vertical-align: bottom;
  width: 115px;
  height: 105px;
  margin-right: 20px;
}
.WanganTitle {
  display: inline-block;
  margin-bottom: 18px;
  margin-left: 0px;
  font-size: calc(2.4vh + 1.5vw);
  line-height: calc(5vh + 2vw);
  text-shadow: rgb(50 50 50 / 80%) 0 3px 10px;
  transform: translateY(calc(-0.5vh));
}
.textLogin {
  position: absolute;
  left: calc(50vw - 400px);
  top: calc(50vh - 100px - (2vh + 2vw));
  width: 800px;
  height: 240px;
  z-index: 1001;
  background: rgba(0, 0, 0, 0.15);
  padding: 30px 0;
  color: #fefefe;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.WanganText {
  font-size: 30px;
  right: 25px;
  text-shadow: rgb(50 50 50 / 60%) 0 1px 10px;
  width: 350px;
}
.loginWanganButton {
  width: 100%;
  margin-top: 10px;
  border: none;
  z-index: 10;
  box-shadow: 0 1px 5px 1px rgb(0 140 238 / 50%);
}
.textCompany {
  position: absolute;
  z-index: 1001;
  transform: translate(-50%, -50%);
  left: 50%;
  bottom: 0.1%;
  color: white;
}

/* 动画 */
@keyframes AllInRight {
  from,
  40%,
  60%,
  85%,
  to {
    -webkit-animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
    animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  }

  from {
    opacity: 0;
    -webkit-transform: translate3d(600px, 0, 0) scaleX(1);
    transform: translate3d(600px, 0, 0) scaleX(1);
  }

  40% {
    opacity: 1;
    -webkit-transform: translate3d(-40px, 0, 0) scaleX(1);
    transform: translate3d(-40px, 0, 0) scaleX(1);
  }

  60% {
    -webkit-transform: translate3d(25px, 0, 0) scaleX(0.98);
    transform: translate3d(25px, 0, 0) scaleX(0.98);
  }

  85% {
    -webkit-transform: translate3d(-10px, 0, 0) scaleX(0.995);
    transform: translate3d(-10px, 0, 0) scaleX(0.995);
  }

  to {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
  }
}
.animate__AllInRight {
  -webkit-animation-name: AllInRight;
  animation-name: AllInRight;
}
</style>
